package com.shimao.celnet.payment.service.impl;

import com.github.pagehelper.PageHelper;
import com.shimao.celnet.payment.form.PPayForm;
import com.shimao.celnet.payment.form.PPayInfoForm;
import com.shimao.celnet.payment.mapper.BHouseMapper;
import com.shimao.celnet.payment.mapper.PPayDetailMapper;
import com.shimao.celnet.payment.mapper.PPayMapper;
import com.shimao.celnet.payment.mapper.PPayRefundMapper;
import com.shimao.celnet.payment.model.PPay;
import com.shimao.celnet.payment.model.PPayDetail;
import com.shimao.celnet.payment.model.request.CallIn_outPayResultRequestPays;
import com.shimao.celnet.payment.service.BHouseService;
import com.shimao.celnet.payment.service.PPayService;
import com.shimao.celnet.payment.util.FinalUtil;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 支付申请信息 管理
 * Created by ensure on 2017/12/3.
 */
@Service
public class PPayServiceImpl extends BaseServiceImpl<PPay> implements PPayService {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired(required = false)
    private PPayDetailMapper pPayDetailMapper;

    @Autowired(required = false)
    private PPayRefundMapper pPayRefundMapper;

    @Autowired(required = false)
    private PPayMapper localMapper;

    @Autowired
    private BHouseService bHouseService;

    /**
     * 分页查询
     * @param formBean
     * @param pageNum
     * @param length
     * @return
     */
    @Override
    public PPayForm getByPage(PPayForm formBean, int pageNum, int length) {
        logger.info("分页查询--开始");
        //分页查询
        Map<String,String> pMap = new HashMap<String,String>();
        //业务类型
        pMap.put("businessType",formBean.getBusinessType());
        //支付类型
        pMap.put("payType",formBean.getPayType());
        //支付房间-模糊查询
        pMap.put("hourceName",'%'+formBean.getHourceName()+'%');
        //支付开始时间
        pMap.put("selStartTime",formBean.getPayStartTime());
        //支付结束时间
        pMap.put("selEndTime",formBean.getPayEndTime());
        //支付状态
        pMap.put("payStatus",formBean.getPayStatus());
        //通联支付流水号-模糊查询
        pMap.put("trxid",'%'+formBean.getTrxid()+'%');

        //是否有退款
        pMap.put("isRefund",formBean.getIsRefund());

        //分页查询
        PageHelper.startPage(pageNum, length);

        List<PPayInfoForm> tableList = localMapper.selectPageInfo(pMap);

        //分页查询
        PageHelper.startPage(pageNum, length);

        formBean.setPageInfo(tableList);
        return  formBean;
    }

    /**
     * 根据外部系统id去重
     * @param payId
     * @return
     */
    @Override
    public PPayInfoForm checkPay(String payId) {
        logger.info("支付详情查询--开始");
        PPayInfoForm retBean = new PPayInfoForm();
        Map<String, String> pMap = new HashMap<String, String>();
        //支付记录guid
        //是否有退款
        pMap.put("payId",payId);

        List<PPayInfoForm> tableList = localMapper.selectInfo(pMap);
        if(tableList.size()>0){
            return tableList.get(0);
        }
        return null;
    }

    /**
     * 支付详情查询
     * @param guid
     * @return
     */
    @Override
    public PPayInfoForm getInfo(String guid) {
        logger.info("支付详情查询--开始");
        PPayInfoForm retBean = new PPayInfoForm();
        Map<String,String> pMap = new HashMap<String,String>();
        //支付记录guid
        pMap.put("guid",guid);

        List<PPayInfoForm> tableList = localMapper.selectInfo(pMap);
        if(tableList.size()>0){
            retBean= tableList.get(0);
        }
        //支付明细设定
         pMap = new HashMap<String,String>();
        //支付记录guid
        pMap.put("payGuid",guid);
        retBean.setPayDetailList(pPayDetailMapper.selectListByMap(pMap));

        //退款明细设定
        pMap.put("payGuid",guid);
        retBean.setPayRefundList(pPayRefundMapper.selectListByMap(pMap));
        logger.info("支付详情查询--结束");
        return  retBean;

    }

    /**
     *新增保存,接口，使用定值FinalUtil.INTER_FACE_USER_ID
     * @param sBean 支付记录
     * @param payDetailList 支付明细
     * @return
     */
    @Override
    public int save(PPay sBean,List<PPayDetail> payDetailList){
        int retCnt=0;
        String loginUserGuid = FinalUtil.INTER_FACE_USER_ID;
        sBean.setUpdateUserGuid(loginUserGuid);
        sBean.setCreatedUserGuid(loginUserGuid);
        //新增付款记录
        retCnt = super.save(sBean);
        //新增付款记录明细
        String payGuid = sBean.getGuid();
        for (PPayDetail pPayDetail : payDetailList) {
            pPayDetail.setGuid(pPayDetailMapper.selectGuid());//支付明细guid
            pPayDetail.setCreatedUserGuid(FinalUtil.INTER_FACE_USER_ID);
            pPayDetail.setUpdateUserGuid(FinalUtil.INTER_FACE_USER_ID);
            pPayDetail.setpPayGuid(payGuid);//支付记录GUID
            //支付申请记录明细保存
            pPayDetailMapper.insert(pPayDetail);
        }

        return retCnt;
    }

    /**
     *修改保存
     * @return
     */
    @Override
    public int update(PPay sBean){
        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        sBean.setUpdateUserGuid(loginUserGuid);
        int retCnt = super.update(sBean);

        return retCnt;
    }

    /**
     *删除数据
     * @return
     */
    @Override
    public  boolean deleteByPayId(String payId,String estateId,String createDate){
        return localMapper.batchDeleteEmps(payId,estateId,createDate);
    }
    /**
     * 外部系统分页查询
     * @param formBean
     * @param pageNum
     * @param length
     * @return
     */
    @Override
    public PPayForm getByPage1(PPayForm formBean, int pageNum, int length) {
        logger.info("分页查询--开始");
        //分页查询
        Map<String,Object> pMap = new HashMap<String,Object>();

        List<String> hourceGuidArray = bHouseService.findhourceGuid('%'+formBean.getHourceName()+'%');
        //支付房间-模糊查询
        pMap.put("hourceGuidArray",hourceGuidArray);
        //支付开始时间
        pMap.put("selStartTime",formBean.getPayStartTime());
        //支付结束时间
        pMap.put("selEndTime",formBean.getPayEndTime());

        //分页查询
        PageHelper.startPage(pageNum, length);

        List<PPayInfoForm> tableList = localMapper.selectInfo1(pMap);

        //分页查询
        PageHelper.startPage(pageNum, length);

        formBean.setPageInfo(tableList);
        return  formBean;
    }

    /**
     * 批量插入记录
     * @return
     */
    @Override
    public void saveByList(List<PPay> saveList) {
        localMapper.saveByList(saveList);
    }



    /**
     *  根据通联支付流水号 详情查询
     * @param trxid
     * @return
     */
    @Override
    public PPayForm getInfoBytrxid(String trxid) {
        logger.info("详情查询--开始");
        //详情查询
        PPayForm formBean = new PPayForm();
        Map<String,String> pMap = new HashMap<String,String>();
        pMap.put("trxid",trxid);//支付账单流水号
        List<PPayInfoForm> tableList = localMapper.selectInfo(pMap);
        PPayInfoForm pPayInfoForm = new PPayInfoForm();
        if(tableList.size()>0) {
            pPayInfoForm = tableList.get(0);
            formBean.setGuid(pPayInfoForm.getGuid());
            formBean.setBusinessType(pPayInfoForm.getBusinessType());
            formBean.setPayType(pPayInfoForm.getPayType());
            formBean.setPayAmount(pPayInfoForm.getPayAmount());
            formBean.setRefundAmount(pPayInfoForm.getRefundAmount());
            formBean.setRefundAmount(pPayInfoForm.getEnRefundAmount());
            formBean.setPayStartTime(pPayInfoForm.getPayStartTime());
            formBean.setPayEndTime(pPayInfoForm.getPayEndTime());
            formBean.setEstateGuid(pPayInfoForm.getEstateGuid());
            formBean.setEstateSfGuid(pPayInfoForm.getEstateSfGuid());
            formBean.setEstateName(pPayInfoForm.getEstateName());
            formBean.setHourceGuid(pPayInfoForm.getHourceGuid());
            formBean.setHourceSfGuid(pPayInfoForm.getHourceSfGuid());
            formBean.setHourceName(pPayInfoForm.getHourceName());
            formBean.setPayUserSfGuid(pPayInfoForm.getPayUserSfGuid());
            formBean.setPayUserName(pPayInfoForm.getPayUserName());
            formBean.setPayStatus(pPayInfoForm.getPayStatus());
            formBean.setPayCustGuid(pPayInfoForm.getPayCustGuid());
            formBean.setQsoCustId(pPayInfoForm.getQsoCustId());
            formBean.setQsoAppId(pPayInfoForm.getQsoAppId());
            formBean.setQsoAppKey(pPayInfoForm.getQsoAppKey());
            formBean.setPayInfoUrl(pPayInfoForm.getPayInfoUrl());
            formBean.setTrxid(pPayInfoForm.getTrxid());
            formBean.setPayRemark(pPayInfoForm.getPayRemark());
            formBean.setAppKey(pPayInfoForm.getAppKey());
            formBean.setInterfaceId(pPayInfoForm.getInterfaceId());
            formBean.setGoBackUrl(pPayInfoForm.getGoBackUrl());
        }

        //支付订单对应支付明细
        Map<String,String> pMapDetail = new HashMap<String,String>();
        pMapDetail.put("payGuid",pPayInfoForm.getGuid());
        System.out.println("==========pPayInfoForm.getGuid()=========="+pPayInfoForm.getGuid());
        List<PPayDetail> payDetailList = pPayDetailMapper.selectListByMap(pMapDetail);

        formBean.setPayDetailList(payDetailList);
        return  formBean;
    }

}
